// studentMine/setInput/setInput.js
import {
    ajaxPost,
    showToast
} from '../../utils/util'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        type: '',
        key: '',
        placeholder: '',
        inputVal: '',
        number: 0,
        show: true,
        start_time: '',
        end_time: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let {
            type,
            val,
            education_type,
            number,
            education_id,
            start_time,
            end_time,
            title,
        } = options
        wx.setNavigationBarTitle({
            title: title
        })
        if (val == null) {
            val = ''
        }
        switch (type) {
            case 'name':
                this.setData({
                    inputVal: val,
                    type: type,
                    key: '昵称',
                    placeholder: '输入您的昵称'
                })
                break
            case 'realname':
                this.setData({
                    inputVal: val,
                    type: type,
                    key: '真实姓名',
                    placeholder: '输入您的真实姓名'
                })
                break
            case 'education':
                if (education_type == 'major') {
                    this.setData({
                        inputVal: val,
                        type: type,
                        education_id: education_id,
                        number: number,
                        education_type: education_type,
                        key: '专业名称',
                        placeholder: '输入您的专业名称'
                    })
                } else if (education_type == 'time') {
                    this.setData({
                        show: false,
                        type: type,
                        education_type: education_type,
                        education_id: education_id,
                        number: number,

                        start_time: start_time,
                        end_time: end_time,
                    })
                } else if (education_type == 'school') {
                    this.setData({
                        type: type,
                        education_id: education_id,
                        number: number,
                        education_type: education_type,
                        key: '学校',
                        placeholder: '输入您的学校名称'
                    })

                }
                break

        }
    },

    /**
     * 
     * input 输入保存
     */
    bindKeyInput(e) {
        this.setData({
            inputVal: e.detail.value
        })
    },
    /**
     * 保存信息
     */
    pushInfo() {
        let {
            number,
            type,
            education_id,
            education_type,
            inputVal,
            end_time,
            start_time,
        } = this.data,
            token = wx.getStorageSync('token'),
            _this = this
        if (type == 'name' || type == 'realname') {
            if (!inputVal) {
                showToast('error', '请填写内容');
                return;
            }
            let data = {

                token: token
            }
            if (type == 'name') {
                data.nickname = inputVal
            } else {
                data.realname = inputVal

            }
            this.setWYNickName(inputVal);
            ajaxPost("/authuser/updateUserInfo", data, (res) => {
                if (res.code == 0) {
                    wx.showToast({
                        title: '保存成功',
                        icon: 'success',
                        duration: 1500,
                        success: () => {
                            setTimeout(() => {
                                wx.navigateBack()
                            }, 1500);
                        }
                    })
                } else {
                    showToast('text', res.msg);
                }
            });
            return;
        }
        if (type == 'education') {
            let data = {
                number: number,
                id: education_id,
                token: token
            }
            if (education_type == 'school') {
                if (!inputVal) {
                    showToast('error', '学校名称不能为空');
                    return;
                }
                data.school = inputVal
                this.educationPushInfo(data)
                return;
            }
            if (education_type == 'major') {
                if (!inputVal) {
                    showToast('error', '专业名称不能为空');
                    return;
                }
                data.major = inputVal
                this.educationPushInfo(data)
                return;
            }
            if (education_type == 'time') {
                if (!start_time || !end_time) {
                    showToast('error', '请选择正确时间段');
                    return;
                }
                data.start_time = start_time
                data.end_time = end_time
                this.educationPushInfo(data)
                return;
            }

        }

    },
    /**
     * 设置网易昵称
     */
    setWYNickName(val) {
        let data = {
            nickname: val,
            token: wx.getStorageSync('token'),
        }
        ajaxPost('/authuser/updateWyUserName', data, () => {});
    },
    /**
     * 学历信息保存
     * 
     */
    educationPushInfo(data) {
        ajaxPost("/authuser/updateEducationExp", data, (res) => {
            if (res.code == 0) {
                wx.showToast({
                    title: '保存成功',
                    icon: 'success',
                    duration: 1500,
                    success: () => {
                        setTimeout(() => {
                            wx.navigateBack()
                        }, 1500);
                    }
                })
            } else {
                showToast('text', res.msg);
            }
        });
    },
    /**
     * 时间改变
     */
    changeData(e) {
        let data = e.currentTarget.dataset
        let type = data.type
        switch (type) {
            case 'start_time':
                /**
                 * 开始时间
                 */
                this.setData({
                    start_time: e.detail.value
                })

                break;
            case 'end_time':
                /**
                 * 结束时间
                 */
                this.setData({
                    end_time: e.detail.value
                })
                break;

        }
    },
})